/**
 * Created by lwy on 2017/7/12.
 */

function getItem(name){
    if (typeof localStorage === 'object') {
        var arr;
        try {
            arr = localStorage.getItem(name);
        } catch (e) {
            arr = document.cookie.match(new RegExp("(^| )" + name + "=([^;]*)(;|$)"));
        }
    }
    if (arr != null) return unescape(arr[2]); return null;
}
function setItem(name,value){
    if (typeof localStorage === 'object') {
        try {
            localStorage.setItem(name, value);
        } catch (e) {
            document.cookie=name+"="+escape(value)+";";
        }
    }
}
function removeItem(name){
    if (typeof localStorage === 'object') {
        try {
            localStorage.removeItem(name);
        } catch (e) {
            var exp = new Date();
            exp.setTime(exp.getTime() - 1);
            var cval = getItem(name);
            document.cookie= name + "="+cval+";expires="+exp.toGMTString() + ";";
        }
    }
}

var flag = true;
var flag2 = 0;
var remember;
//点击验证码登录
$('#codelogin').click(function(){
    $('#passloginbox').hide();
    $('#codeloginbox').show();
    $('#login-button2').show();
    $('#phoneImg').attr('src','../img/login2-user2.png');
    $('#phoneImg').css({'width':'12px','height':'18px'})
    $('.code-button').show();
    $('#login-button1').hide();
    $(this).addClass('login2-active');
    $(this).siblings().removeClass('login2-active');
})
//点击账号密码登录
$('#passlogin').click(function(){
    $('#codeloginbox').hide();
    $('#passloginbox').show();
    $('#login-button1').show();
    $('#phoneImg').attr('src','../img/login2-user.png');
    $('#phoneImg').css({'width':'14px','height':'17px'})
    $('.code-button').hide();
    $('#login-button2').hide();
    $(this).addClass('login2-active');
    $(this).siblings().removeClass('login2-active');
})

//回车登录
function keyLogin(){
    if (event.keyCode==13){
        if($('#login-button1').is(':hidden')){
            document.getElementById("login-button2").click(); //调用登录按钮的登录事件
        }else if($('#login-button2').is(':hidden')){
            document.getElementById("login-button1").click(); //调用登录按钮的登录事件
        }
    }
}

//账号密码登录
function loginButton1(){
    if(flag!=true){
        mui.toast('正在登录，请稍候...');
        return;
    }
    flag=false;
    var userName = $('#phone').val();
    var passWord = $('#password').val();
    var  myArray=[];
    myArray[0]=userName;
    myArray[1]=passWord;
    var  result = ifEmpty(myArray);
    if(result==0){
        mui.toast('用户名和密码不能为空');
        flag=true;
        return;
    }
    $.ajax({
        url:'/user/userLoginKey.json',
        dataType:'json',
        data:{
            name:userName,
        },
        success:function(data){
            if(data.code!=1){
                mui.toast(data.msg);
                flag=true;
                return;
            }
            if(data.code==1){
                passWord= calcMD5(passWord + data.datas.userKey, 32);
                $.ajax({
                    url:'/user/userLogin.json',
                    dataType:'json',
                    type:'post',
                    data:{
                        userName:userName,
                        passWord:remember ? localStorage.getItem('passWord') :passWord,
                    },
                    success:function(data){
                        if(data.code!=1){
                            mui.toast(data.msg);
                            flag=true;
                            return;
                        }
                        if(data.code==1){
                                setItem('token',data.datas.token);
                                setItem('phone',userName);
                                setItem('passWord',remember ? localStorage.getItem('passWord') : passWord);
                                window.location.href="index.html";
                        }
                    }
                })
            }
        }
    })
}

//验证码登录
function loginButton2(){
    if(flag2!=0){
        mui.toast("正在请求中，请稍候");
        return;
    }
    if($('#phone').val()==""){
        mui.toast('请输入正确手机号');
        return;
    }
    if($('.code-input').val().length!=6){
        mui.toast('请输入正确验证码');
        return;
    }
    var str = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
    var n = 5, s = "";
    for(var i = 0; i < n; i++){
        var rand = Math.floor(Math.random() * str.length);
        s += str.charAt(rand);
    }

    flag2=1;
    $.ajax({
        url:'/user/checkMobileCode.json',
        data:{
            mobile:$('#phone').val(),
            mobile_code:$('.code-input').val(),
            platform:'wap',
        },
        dataType:'json',
        success:function(data) {
            if(data.code!=1){
                flag2=0;
                mui.toast(data.msg);

            }else{
                $.ajax({
                    url:'/user/userLogin2.json',
                    data:{
                        userName:$('#phone').val(),
                    },
                    dataType:'json',
                    success:function(data){
                        if(data.code!=1){
                            flag2=0;
                            mui.toast(data.msg);

                        }else{
                            localStorage.setItem('token',data.datas.token);
                            localStorage.setItem('phone',$('#phone').val());
                            window.location.href='index.html';
                        }

                    }
                })
            }

        }
    });
}

//发送验证码
$('.code-button').click(function(){
    sendCode();
})
function sendCode(){
    if(!$('#phone').val().match(/^1[2-9][0-9]{9}$/)){
        mui.toast('请输入正确手机号码');
        return;
    }
    $(".code-button").html("发送中…");
    $("#codeloginbox").unbind();
    $.ajax({
        url:'/user/sendMobileCode.json',
        data:{
            mobile:$('#phone').val(),
            platform:'wap',

        },
        dataType:'json',
        success:function(data) {
            if(data.code!=1){
                mui.toast(data.msg);
                $(".code-button").html("重新发送");
                $("#codeloginbox").bind('click',sendCode);
                return;
            }
            timeUpdate.init($(".code-button"), 60);

        }
    })

}

var timeUpdate = {
    node:null,//节点
    time:60,//时间
    //主函数
    start:function(){
        if(this.time > 0){
            $(this.node).html( this.time-- + 's后重发');
            var _this = this;
            setTimeout(function(){
                _this.start();
            },1000);
        }else{
            this.node.click(function () {
                sendCode();
            });
            this.node.html("重新发送");
        }
    },
    //初始化
    init:function(node, timeLimit){
        this.node = node;
        this.time = timeLimit;
        this.start();
    }
};
//浏览器有填充,记住密码
$(function () {
    var name = localStorage.getItem('phone');
    var pass = localStorage.getItem('passWord');
    if (pass != null) {
        remember = true;
        $('#phone').val(name);
        //$('#password').val(pass);
        $('#password').val('15678455545');
        $('.eye').hide();
    } else {
        remember = false;
    }

});
$('#phone').keydown(function () {
    remember = false;
});
$('#password').keydown(function () {
    remember = false;
});

//清除按钮隐藏显示
document.getElementById('phone').oninput=function(){
    //document.activeElement.scrollIntoViewIfNeeded();
    if($(this).val()==""||$(this).val()=='请输入手机号'){
        $('.clear').hide();
    }else{
        $('.clear').show();
    }
    if($(this).val().length==11){
        $('#password').focus();
    }
}
//清除按钮清除输入框内容
$('.clear').click(function(){
    $('#phone').val('');
})
//密码明文
$('.eye').click(function(){
    if($('#password').attr('type')=='text'){
        $('#password').attr('type','password');
        $('.eye').css({'background':'url("../img/eye-hide.png") no-repeat 0 0','background-size':'17px 8.5px','width':'17px','height':'8.5px','top':'26px'})
    }else{
        $('#password').attr('type','text');
        $('.eye').css({'background':'url("../img/eye-show.png") no-repeat 0 0','background-size':'18px 9px','width':'18px','height':'9px','top':'22px'})
    }
})
//同意协议
$('#agreeicon').click(function(){
    if($(this).attr('src')=='../img/agree.png'){
        $(this).attr('src','../img/noAgree.png');
        $('#login-button1').css('pointer-events','none');
        $('#login-button2').css('pointer-events','none');
    }else{
        $(this).attr('src','../img/agree.png');
        $('#login-button1').css('pointer-events','auto');
        $('#login-button2').css('pointer-events','auto');
    }
})